﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZK.MyControl
{
    public class RichTextBoxPanel:RichTextBox
    {

        public RichTextBoxPanel()
        {
            this.Multiline = true;  //将Multiline属性设为true，实现显示多行
            this.ScrollBars = RichTextBoxScrollBars.Vertical;  //设置ScrollBars属性实现只显示垂直滚动条

        }

        #region 日志记录、支持其他线程访问 
        public delegate void LogAppendDelegate(Color color, string text);

        public delegate void LogClearDelegate();
        /// <summary> 
        /// 追加显示文本 
        /// </summary> 
        /// <param name="color">文本颜色</param> 
        /// <param name="text">显示文本</param> 
        public void LogAppend(Color color, string text)
        {

            this.SelectionStart = this.Text.Length;
            this.SelectionColor = color;
            this.AppendText(text);
            this.AppendText("\n");
            //滚动到控件光标处
            this.ScrollToCaret();
            
        }
        /// <summary> 
        /// 显示错误日志 
        /// </summary> 
        /// <param name="text"></param> 
        public void LogError(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            this.Invoke(la, Color.Red, DateTime.Now.ToString("HH:mm:ss ") + text);
        }
        /// <summary> 
        /// 显示警告信息 
        /// </summary> 
        /// <param name="text"></param> 
        public void LogWarning(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            this.Invoke(la, Color.Blue, DateTime.Now.ToString("HH:mm:ss ") + text);
        }

        public void ShowDebug(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            this.Invoke(la, Color.Blue,  text);
        }

        public void ShowError(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            this.Invoke(la, Color.Red, text);
        }

        /// <summary> 
        /// 显示信息 
        /// </summary> 
        /// <param name="text"></param> 
        public void LogMessage(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            this.Invoke(la, Color.Black, DateTime.Now.ToString("HH:mm:ss ") + text);
        }

        /// <summary> 
        /// 显示信息 
        /// </summary> 
        /// <param name="text"></param> 
        public void AppendMessage(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            this.Invoke(la, Color.Black,  text);
        }




        public void TxtClear()
        {
            if (this.InvokeRequired)
            {
                LogClearDelegate lc = new LogClearDelegate(TxtClear);
                {
                    this.Invoke(lc);
                }

            }
            else {
                this.Clear();
            }
           
        }
        #endregion


    }
}
